//
//  BMKCircleAnnotationView.m
//  RNIbuildModuleBaiduMap
//
//  Created by Wentao Ye on 22/05/2017.
//  Copyright © 2017 Facebook. All rights reserved.
//

#import "BMKProjectInfoAnnotationView.h"

@implementation BMKProjectInfoAnnotationView

-(instancetype)init{
    self = [super init];
    if (self) {
        
    }
    return  self;
}
-(id)initWithAnnotation:(id<BMKAnnotation>)annotation reuseIdentifier:(NSString *)reuseIdentifier
{
    if ((self = [super initWithAnnotation:annotation reuseIdentifier:reuseIdentifier])) {
        [self setFrame:CGRectMake(0, 0, kWidth(28), kWidth(28))];
        [self setUserInteractionEnabled:YES];
        self.backgroundColor = [UIColor clearColor];
        [self setUserInteractionEnabled:YES];
        
        
        
        
        
        self.bgIcon = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth(28), kHeight(28))];
        [self addSubview:self.bgIcon];
        [self.bgIcon setUserInteractionEnabled:YES];
        UITapGestureRecognizer *tapRecg = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
        [self.bgIcon addGestureRecognizer:tapRecg];
        
        self.bgTitleArrow = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kWidth(10), kHeight(7))];
        self.bgTitleArrow.image = [UIImage imageNamed:@"icon_projectname_triangle"];
        
        UIImage *img = [UIImage imageNamed:@"icon_projectname_rectangle"];
        img = [img resizableImageWithCapInsets:UIEdgeInsetsMake(0, img.size.width * 0.5 ,0, img.size.width * 0.5) resizingMode:UIImageResizingModeStretch];
        self.bgTitle = [[UIImageView alloc] initWithFrame:CGRectMake(-((kWidth(132) / 2.0 - kWidth(28) / 2.0)), 0, 0, kHeight(27))];
        
        self.bgTitle.image = img;
        
        [self addSubview:self.bgTitle];
        [self addSubview:self.bgTitleArrow];
        self.lblProjectName = [[UILabel alloc] initWithFrame:CGRectMake(10, 3, 0, self.bgTitle.frame.size.height - 10)];
        self.lblProjectName.textColor = [ Util  colorWithHexString:@"#333333"];
        self.lblProjectName.numberOfLines = 1;
        self.lblProjectName.textAlignment = NSTextAlignmentCenter;
        self.lblProjectName.lineBreakMode = NSLineBreakByTruncatingTail;
        self.lblProjectName.font = [UIFont systemFontOfSize:12.f];
        self.lblProjectName.backgroundColor = [UIColor clearColor];
        [self.bgTitle addSubview:self.lblProjectName];
        
        [self.bgTitle setUserInteractionEnabled:YES];
        [self.lblProjectName setUserInteractionEnabled:YES];
        
        UITapGestureRecognizer *tapRecg2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(viewTapped:)];
        [self.bgTitle addGestureRecognizer:tapRecg2];
    }
    return self;
}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    UIView *view = [super hitTest:point withEvent:event];
    if (view == nil) {
        CGPoint tempoint = [self.bgTitle convertPoint:point fromView:self];
        if (CGRectContainsPoint(self.bgTitle.bounds, tempoint))
        {
            view = self.bgTitle;
        }
    }
    return view;
}

-(void)viewTapped:(UITapGestureRecognizer *)sender
{
    [self.delegate BMKProjectInfoAnnotationView:self clickViewWithData:self.annotation];
}

-(void) displayProjectName:(NSString *)projectName
{
    if(projectName)
    {
        CGSize size = [projectName sizeWithFont:self.lblProjectName.font constrainedToSize:CGSizeMake(kWidth(220),  self.lblProjectName.frame.size.height) lineBreakMode:NSLineBreakByTruncatingTail];
        self.bgTitle.frame = CGRectMake(-(((size.width + 20.f) / 2.0 - kWidth(28) / 2.0)), -kHeight(27), size.width + 20.f, self.bgTitle.frame.size.height);
        self.lblProjectName.text = projectName;
        self.lblProjectName.frame = CGRectMake(self.lblProjectName.frame.origin.x, (self.bgTitle.frame.size.height - size.height) / 2.f ,size.width, size.height);
        self.bgTitleArrow.frame = CGRectMake(kWidth(14.f)-self.bgTitleArrow.frame.size.width / 2.f, self.bgTitle.frame.origin.y + self.bgTitle.frame.size.height - kHeight(4), kWidth(10), kHeight(7));
    }
}
@end
